package com.dragonfly.shutter.services;

import java.util.Date;

import com.dragonfly.shutter.DAOs.AlbumDAO;
import com.dragonfly.shutter.DAOs.BrowseLogDAO;
import com.dragonfly.shutter.DAOs.PictureAlbumDAO;
import com.dragonfly.shutter.DAOs.PictureDAO;
import com.dragonfly.shutter.beans.Album;
import com.dragonfly.shutter.beans.BrowseLog;
import com.dragonfly.shutter.beans.PictureAlbum;

/**
 * 复制图片服务类
 * @author 黄挺
 *
 */
public class CopyPictureService {
	private BrowseLogDAO browseLogDAO;
	private PictureAlbumDAO pictureAlbumDAO;
	private AlbumDAO albumDAO;
	private PictureDAO pictureDAO; 
	
	/**
	 * 复制图片服务类
	 * @param albumID 相册ID
	 * @param pictureID 图片ID
	 * @param userID 用户ID
	 * @param requestURL 请求URL
	 */
	public void doService(Integer albumID, String pictureID, Integer userID,
			String requestURL){
		BrowseLog browseLog = null;
		PictureAlbum pictureAlbum = new PictureAlbum();
		
		//生成日志Bean
		if(userID == null )
			browseLog = new BrowseLog(new Date(System.currentTimeMillis()), requestURL);
		else
			browseLog = new BrowseLog(userID, new Date(System.currentTimeMillis()), requestURL);
		
		pictureAlbum.setAlbumID(albumID);
		pictureAlbum.getPictureBrief().setPictureID(pictureID);
		
		doDBOperate(browseLog,pictureAlbum);
	}
	
	/**
	 * 复制图片服务函数
	 * @param browseLog 日志Bean
	 * @param pictureAlbum 图片相册关联Bean
	 */
	public void doDBOperate(BrowseLog browseLog,PictureAlbum pictureAlbum){
		
		pictureAlbumDAO.insertPictureAlbum(pictureAlbum);
		Album album = albumDAO.selectAlbumByID(pictureAlbum.getAlbumID());
		
		if(album.getPictureCount() == 0 ){
			String thumbLocation =pictureDAO.selectPicBriefByPicId(pictureAlbum.getPictureBrief().getPictureID()).getThumbLocation();
			albumDAO.updateEnvelopPicId(album.getAlbumID(), 
					thumbLocation);
		}
		album.setPictureCount(album.getPictureCount() + 1);
		albumDAO.updatePictureCount(album);
		browseLogDAO.insertBrowseLog(browseLog);
	}
	
	public void setBrowseLogDAO(BrowseLogDAO browseLogDAO) {
		this.browseLogDAO = browseLogDAO;
	}
	
	public void setPictureAlbumDAO(PictureAlbumDAO pictureAlbumDAO) {
		this.pictureAlbumDAO = pictureAlbumDAO;
	}

	public void setAlbumDAO(AlbumDAO albumDAO) {
		this.albumDAO = albumDAO;
	}

	public AlbumDAO getAlbumDAO() {
		return albumDAO;
	}

	public void setPictureDAO(PictureDAO pictureDAO) {
		this.pictureDAO = pictureDAO;
	}
}
